﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using HawaiRestaurant.Models.Entities;
using Microsoft.Office.Interop.Word;

namespace HawaiRestaurant.Models.Helper
{
    public class DocumentHelper
    {
        public void GenerateDoc(string template, string newFile,DocumentEntities entities)
        {
            //  create offer letter
            try
            {
                //  Just to kill WINWORD.EXE if it is running
                Process[] process = Process.GetProcessesByName("winword");
                foreach (Process process1 in process)
                {
                    process1.Kill();
                }

                //  copy letter format to temp.doc
                File.Copy(template, newFile, true);

                //  create missing object
                object missing = Missing.Value;

                //  create & define filename object with temp.doc
                object filename = newFile;

                //  if temp.doc available
                if (File.Exists((string) filename))
                {
                    object readOnly = false;
                    object isVisible = false;

                    //  create Word application object
                    var wordApp = new Application {Visible = false};
                    
                    //  open Word document named temp.doc
                    Document aDoc = wordApp.Documents.Open(ref filename, ref missing,
                                                           ref readOnly, ref missing, ref missing, ref missing,
                                                           ref missing, ref missing, ref missing, ref missing,
                                                           ref missing, ref isVisible, ref missing, ref missing,
                                                           ref missing, ref missing);
                    aDoc.Activate();

                    //  Save data
                    SaveData(wordApp, entities);

                    //  save temp.doc after modified
                    aDoc.Save();
                }
                else
                {
                    // Todo
                }
            }
            catch (Exception)
            {
                //Todo
            }
        }

        private void FindAndReplace(Application wordApp, object findText, object replaceText)
        {
            object matchCase = true;
            object matchWholeWord = true;
            object matchWildCards = false;
            object matchSoundsLike = false;
            object matchAllWordForms = false;
            object forward = true;
            object format = false;
            object matchKashida = false;
            object matchDiacritics = false;
            object matchAlefHamza = false;
            object matchControl = false;
            object replace = 2;
            object wrap = 1;
            wordApp.Selection.Find.Execute(ref findText, ref matchCase,
                                           ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
                                           ref matchAllWordForms, ref forward, ref wrap, ref format,
                                           ref replaceText, ref replace, ref matchKashida,
                                           ref matchDiacritics,
                                           ref matchAlefHamza, ref matchControl);
        }

        private void SaveData(Application wordApp, DocumentEntities entities)
        {
            switch (entities.DocumentType)
            {
                case "ORDERPARTYCONFIRM":
                    foreach (var item in entities.ListData)
                    {
                        // Find and replace
                        FindAndReplace(wordApp, item.Key, item.Value);
                    }
                    break;
                default:
                    //Todo
                    break;
            }
        }
    }
}